﻿/*
 Author:du
 Time:2017.10.30
*/

using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System;
using System.Reflection;
using FutureCoreEditor;

[InitializeOnLoad]
public static class UGUIMenuExtendTool
{
    static MethodInfo m_miGetDefaultResource = null;
    static MethodInfo m_miPlaceUIElementRoot = null;

    static UGUIMenuExtendTool()
    {
        Initialize();
    }

    static void Initialize()
    {
        Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
        Assembly uiEditorAssembly = null;
        foreach (Assembly assembly in allAssemblies)
        {
            AssemblyName assemblyName = assembly.GetName();
            if ("UnityEditor.UI" == assemblyName.Name)
            {
                uiEditorAssembly = assembly;
                break;
            }
        }

        if (null == uiEditorAssembly)
        {
            Debug.LogError("Can not find assembly: UnityEditor.UI.dll");
            return;
        }

        Type menuOptionType = uiEditorAssembly.GetType("UnityEditor.UI.MenuOptions");
        m_miGetDefaultResource = menuOptionType.GetMethod("GetStandardResources", BindingFlags.NonPublic | BindingFlags.Static);
        m_miPlaceUIElementRoot = menuOptionType.GetMethod("PlaceUIElementRoot", BindingFlags.NonPublic | BindingFlags.Static);
    }

    public static void UIRootInvoke(GameObject go, MenuCommand menuCommand)
    {
        m_miPlaceUIElementRoot.Invoke(null, new object[] { go, menuCommand });
    }

    [MenuItem("GameObject/[FC UI]/[Project]/UIView", false, 9)]
    public static void AddScreenPanel(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreatePanel((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        RectTransform rtf = go.GetComponent<RectTransform>();
        rtf.anchoredPosition = Vector2.zero;
        rtf.sizeDelta = Vector2.zero;

        Image image = go.GetComponent<Image>();
        image.raycastTarget = false;

        rtf.sizeDelta = EPAppConst.ScreenResolution;
        Vector2 half = new Vector2(0.5f, 0.5f);
        rtf.anchorMin = half;
        rtf.anchorMax = half;

        CanvasRenderer canvasRender = go.GetComponent<CanvasRenderer>();
        UnityEngine.Object.DestroyImmediate(image);
        UnityEngine.Object.DestroyImmediate(canvasRender);
        go.name = "UIView";
    }

    [MenuItem("GameObject/[FC UI]/Text", false, 10)]
    public static void AddText(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateText((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Text text = go.GetComponent<Text>();
        text.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Image", false, 11)]
    public static void AddImage(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateImage((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Image image = go.GetComponent<Image>();
        image.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Raw Image", false, 12)]
    public static void AddRawImage(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateRawImage((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        RawImage rawImage = go.GetComponent<RawImage>();
        rawImage.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Button", false, 13)]
    public static void AddButton(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateButton((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Text text = go.transform.Find("Text").GetComponent<Text>();
        text.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Toggle", false, 14)]
    public static void AddToggle(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateToggle((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Text text = go.transform.Find("Label").GetComponent<Text>();
        text.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Slider", false, 15)]
    public static void AddSlider(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateSlider((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Image Image1 = go.transform.Find("Background").GetComponent<Image>();
        Image Image2 = go.transform.Find("Fill Area/Fill").GetComponent<Image>();
        Image1.raycastTarget = false;
        Image2.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Scrollbar", false, 16)]
    public static void AddScrollbar(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateScrollbar((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Image image = go.GetComponent<Image>();
        image.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Dropdown", false, 17)]
    public static void AddDropdown(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateDropdown((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Text text = go.transform.Find("Label").GetComponent<Text>();
        text.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/InputField", false, 18)]
    public static void AddInputField(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateInputField((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Text text1 = go.transform.Find("Placeholder").GetComponent<Text>();
        Text text2 = go.transform.Find("Text").GetComponent<Text>();
        text1.raycastTarget = false;
        text2.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Panel", false, 19)]
    public static void AddPanel(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreatePanel((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        RectTransform rtf = go.GetComponent<RectTransform>();
        rtf.anchoredPosition = Vector2.zero;
        rtf.sizeDelta = Vector2.zero;

        Image image = go.GetComponent<Image>();
        image.raycastTarget = false;
    }

    [MenuItem("GameObject/[FC UI]/Scroll View", false, 20)]
    public static void AddScrollView(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateScrollView((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        UIRootInvoke(go, menuCommand);

        Image image = go.GetComponent<Image>();
        image.raycastTarget = false;
    }
}